package com.hopu.v1.controller.admin.classes;



import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.v1.pojo.classes.CourseHomeworkRecord;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.common.Update;
import com.hopu.v1.service.admin.classes.CourseHomeworkRecordService;
import com.hopu.v1.vo.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 学生课程作业记录表(CourseHomeworkRecord)表控制层
 *
 * @author makejava
 * @since 2024-09-29 16:54:55
 */
@RestController
@RequestMapping("courseHomeworkRecord")
public class CourseHomeworkRecordController  {
    /**
     * 服务对象
     */
    @Resource
    private CourseHomeworkRecordService courseHomeworkRecordService;

    @GetMapping("select")
    public R selectAll(Page<CoursehomeworkVo> page,String classId) {
        return R.ok(this.courseHomeworkRecordService.selectcourseWork(page, classId));
    }

    @GetMapping("selectStudentWork")
    public R selectStudentWork(Page<WorkRecordVo> page, String classId,String courseName) {
        return R.ok(this.courseHomeworkRecordService.selectStudentWork(page, classId, courseName));
    }

    @GetMapping("selectCheckRecord")
    public R selectCheckRecord(String courseName,String studentId) {


        return R.ok(this.courseHomeworkRecordService.selectCheckRecord(courseName, studentId));
    }

    @PostMapping ("update")
    public R update(@Validated() @RequestBody CorrectingVo[] correctingVos){
        for (CorrectingVo record : correctingVos) {
            if (record.getJudgment() == null) {
                return R.ok("选项不能为空");
            }
        if (record.getTeacherComments().length() > 255){
            return R.ok("教师评语不能超过255个字符");}
        }
        for (CorrectingVo record : correctingVos) {
            LambdaUpdateWrapper<CourseHomeworkRecord> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(CourseHomeworkRecord::getStudentId,record.getStudentId());
            //其中的homeworkid 换成数组对象
            wrapper.eq(CourseHomeworkRecord::getHomeworkId,record.getHomeworkId());
            //其中的JudgmentList 换成前端传过来的 对错数组   comments 换成前端传过来的评语
            wrapper.set(CourseHomeworkRecord::getCorrectJudgment,record.getJudgment()).set(CourseHomeworkRecord::getTeacherComments,record.getTeacherComments());
            courseHomeworkRecordService.update(wrapper);
        };
        return R.ok("修改成功");
    }



    /**
     * 添加作业
     * @return
     */
    @PostMapping("addStudentWork")
    public R addStudentWork(@RequestParam List<String> studentIds, @RequestParam List<String> homeworkIds) {
        ArrayList<Object> objects = new ArrayList<>();
        homeworkIds.forEach(homeworkId -> {
            studentIds.forEach(studentId -> {
                // 检查数据库中是否已经存在该学生 ID 和作业 ID 的记录
                QueryWrapper<CourseHomeworkRecord> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("student_id", Long.valueOf(studentId))
                        .eq("homework_id", Long.valueOf(homeworkId));
                if (!courseHomeworkRecordService.exists(queryWrapper)) {
                    CourseHomeworkRecord courseHomeworkRecord = new CourseHomeworkRecord();
                    courseHomeworkRecord.setStudentId(Long.valueOf(studentId));
                    courseHomeworkRecord.setHomeworkId(Long.valueOf(homeworkId));
                    courseHomeworkRecordService.save(courseHomeworkRecord);
                    objects.add(courseHomeworkRecord);
                }
            });
        });
        if (objects.size() > 0) {
            return R.ok("添加成功");
        }else {
            return R.ok("添加失败");
        }
    }

    @GetMapping("selectCourse")
    public R selectCourse(String classId,String pid) {
        return R.ok("nb",this.courseHomeworkRecordService.selectCourse(classId, pid));
    }

    /**
     * 分页查询所有数据
     *
     * @param
     * @param
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public R selectAll(CourseHomeworkRecord courseHomeworkRecord, Page<CourseHomeworkRecord> page) {
        return R.ok(this.courseHomeworkRecordService.page(page, new QueryWrapper<>(courseHomeworkRecord)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.courseHomeworkRecordService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param courseHomeworkRecord 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody CourseHomeworkRecord courseHomeworkRecord) {
        return R.ok(this.courseHomeworkRecordService.save(courseHomeworkRecord));
    }

    /**
     * 修改数据
     *
     * @param courseHomeworkRecord 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody CourseHomeworkRecord courseHomeworkRecord) {
        return R.ok(this.courseHomeworkRecordService.updateById(courseHomeworkRecord));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.courseHomeworkRecordService.removeByIds(idList));
    }
}

